//
//  DataBaseEntity.m
//  MovingLoaniOS
//
//  Created by Rain on 2018/8/23.
//  Copyright © 2018 - 2019 GWMFC. All rights reserved.
//

#import "DataBaseEntity.h"
#import "RGDataBase.h"

@implementation DataBaseEntity

+ (NSMutableArray *)getFormattedEntitiesFor:(NSArray *)firstClassEntities
                               fromEntities:(NSArray *)allEntities
{
    NSMutableArray *entities = [NSMutableArray array];
    for (DataBaseEntity *entity in firstClassEntities) {
        entity.subEntities = [NSMutableArray array];
        for (DataBaseEntity *subEntity in allEntities) {
            if ([subEntity.pCode isEqualToString:entity.code]) {
                [entity.subEntities addObject:subEntity];
            } else {
                continue;
            }
        }
        [entities addObject:entity];
    }
    return entities;
}

+ (NSMutableArray *)getFirstClassEntitiesByTypeID:(NSString *)typeID {
    return [[self class] getEntitiesByPcode:@"0" inEntities:[[self class] getAllEntitiesByTypeID:typeID]];
}

+ (NSMutableArray *)getEntitiesByCode:(NSString *)code
                           inEntities:(NSArray *)entitiesInSearch
{
    NSMutableArray *entities = [NSMutableArray array];
    for (DataBaseEntity *entity in entitiesInSearch) {
        if ([entity.code isEqualToString:code]) {
            [entities addObject:entity];
        } else {
            continue;
        }
    }
    return entities;
}

+ (NSMutableArray *)getEntitiesByPcode:(NSString *)pcode
                            inEntities:(NSArray *)entitiesInSearch
{
    NSMutableArray *entities = [NSMutableArray array];
    for (DataBaseEntity *entity in entitiesInSearch) {
        if ([entity.pCode isEqualToString:pcode]) {
            [entities addObject:entity];
        } else {
            continue;
        }
    }
    return entities;
}

+ (NSMutableArray *)getAllEntitiesByTypeID:(NSString *)typeID {
    FMResultSet *results = [[RGDataBase sharedDataBase] resultSetByTypeID:typeID];
    NSMutableArray *array = [NSMutableArray array];
    while (results.next) {
        DataBaseEntity *entity = [[DataBaseEntity alloc] init];
        entity.typeID = [results stringForColumn:@"TYPEID"];
        entity.code = [results stringForColumn:@"CODE"];
        entity.pCode = [results stringForColumn:@"PCODE"];
        entity.name = [results stringForColumn:@"NAME"];
        entity.param = [results stringForColumn:@"param"];
        [array addObject:entity];
    }
    return array;
}

+ (NSString *)getNameByCode:(NSString*)code WithTypeID:(NSString *)typeID{
    
  NSMutableArray *resultArray = [self getAllEntitiesByTypeID:typeID];
    
    NSString *name = @"";
    for (DataBaseEntity *model in resultArray) {
        if ([model.code isEqualToString:code]) {
            name = model.name;
        }
    }
    return name;
    
}

+ (NSString *)getCodeByName:(NSString*)name WithTypeID:(NSString *)typeID{
    
    NSMutableArray *resultArray = [self getAllEntitiesByTypeID:typeID];
    
    NSString *code = @"";
    for (DataBaseEntity *model in resultArray) {
        if (name && [model.name isEqualToString:name]) {
            code = model.code;
        }
    }
    return code;
    
}

@end
